# -*- coding: utf-8 -*-
# @Date   : 2021/07/15
# @File   : CF19_XECurrencyOfUSD.py
# @Author : zhanghengfeng

'''eof
name:实时美元汇率
code:CF19_XECurrencyOfUSD
tableName:
columnName:
groups:出口信息模块
dependencies:CF_CQ_DSJ
type:常用指标
dataSourceType:
eof'''

import json

null_type_list = ['', ' ', None, 'null', 'Null', 'NULL', '/', '-', '--']

def isfloat(x):
    '''
    判断x为float类型且大于等于0
    :param x:
    :return:
    '''
    try:
        if isinstance(float(x), float) and float(x)>=0:
            if str(x) in ['inf', 'infinity', 'INF', 'INFINITY', 'True']:
                return False
            else:
                return True
        else:
            return False
    except:
        return False


def CF19_XECurrencyOfUSD():
    try:
        #汇率表
        rate = json.loads(ratingRequest.get("ELoanTradeList"))[0].get('rate')
        if rate in null_type_list or rate == []:
            return u'缺失值'
        elif isinstance(rate,list) and len(rate)>0:
            errorConts = 0
            missConts = 0
            RATE = 0
            for i in rate:
                try:
                    #币种
                    currency = i.get("currency")
                    #汇率
                    rateTem = i.get("rate")
                    if isfloat(currency) and isfloat(rateTem) and len(currency) == 2:
                        if currency == "04":
                            RATE += float(rateTem)
                            break
                        else:
                            missConts += 1
                    else:
                        errorConts += 1
                except:
                    return u'缺失值'
            if errorConts + missConts == len(rate):
                return u'缺失值'
            else:
                return RATE
        else:
            return u'缺失值'
    except:
        return u'缺失值'                  
result = CF19_XECurrencyOfUSD()